var roles = [ {id : '1',text : '管理员'},{id : '2',text : '校长'},{id : '3',text : '教师'},{id : '4',text : '学生'}];
var depts = [ {id : '1',text : '一年级'},{id : '2',text : '二年级'},{id : '3',text : '三年级'},{id : '4',text : '四年级'},{id : '5',text : '五年级'},{id : '6',text : '六年级'}];
var statuss = [ {id : '1',text : '在学'},{id : '2',text : '休学'},{id : '3',text : '其他'},{id : '4',text : '毕业'}];
var sexs = [{id:'1',text:'男'},{id:'女'}];
$(function(){
var gridfilter={};


	// 列表结构
	 var grid = $("#maingrid").ligerGrid(
					{
						columns : [ 
								            {display : "姓名",name : "username",width : 150,type : "text",align : "center"},
								            
								            {display : "性别",name : "sex",width : 150,type : "text",align : "center", 
												render: function(item){
													var html = '';
													for (var i = 0; i < sexs.length; i++){
												        if (sexs[i]['id'] == item['sex']){
												        	html = sexs[i]['text'];
												        	break;
												        }
												    }
													return html;
												}},
								            {display : "年级",name : "dept",width : 120,type : "text",align : "center", 
												render: function(item){
													var html = '';
													for (var i = 0; i < depts.length; i++){
												        if (depts[i]['id'] == item['dept']){
												        	html = depts[i]['text'];
												        	break;
												        }
												    }
													return html;
												}},
								            {display : "班级",name : "class_",width : 150,type : "date",align : "center"},
								            {display : "民族",name : "nature",width : 150,type : "text",align : "center"},
								            {display : "身份证号",name : "idcode",width : 150,type : "text",align : "center"},
								            {display : "政治面貌",name : "zcode",width : 150,type : "text",align : "center"},
								            {display : "入学年份",name : "rxny",width : 150,type : "text",align : "center"},
								            {display : "出生日期",name : "birthday",width : 150,type : "text",align : "center"},
								            {display : "学生状态",name : "status",width : 150,type : "text",align : "center",
								            	render: function(item){
													var html = '';
													for (var i = 0; i < statuss.length; i++){
												        if (statuss[i]['id'] == item['status']){
												        	html = statuss[i]['text'];
												        	break;
												        }
												    }
													return html;
												}}
												
										  ],
						dataAction : 'server',
						pageSize : 20,
						sortName : "usercode",
						delayLoad:true,
						toolbar : {},
						url : basepath + 'SystemWeb/getGrid.do?resource=getStudy',
						width : '98%',
						height : '100%',
						heightDiff : -10,
						rownumbers:false,
						checkbox : false,
						dataFilter: gridfilter,
						parms :{where : JSON2.stringify(gridfilter)}
					});
	 grid.loadData(true);
	 $(".l-panel-bbar-inner").hide();
	// 工具条事件
	  function toolbarBtnItemClick(item) {
			switch (item.id) {
			case "add":
				showDetail({
						usercode:'',
						nature:'',
						idcode:'',
						zcode:'',
						status:'',
						rxny:'',
						birthday:''
				},true,false);
				break;
			
			case "modify" :
				var selected = grid.getSelected();
				if(!selected){
					LG.tip("请选择行");
					return
				}
				showDetail({
					usercode:selected.usercode,
					nature:selected.nature,
					password:selected.password,
					phone:selected.phone,
					sex:selected.sex,
					class_:selected.class_,
					role:selected.role,
					dept:selected.dept
			},false,false);
				break;
			case "delete":
				var selected = grid.getSelected();
				if (!selected) {
					LG.tip('请选择行!');
					return
				}
				
				jQuery.ligerDialog.confirm('确定删除吗?', function(confirm) {
					if (confirm)
						f_delete(selected.usercode);
				});
				break;
			
			}
	  }
	  var detailWin = null, curentData = null, isView=null,isAdd=null;
		function showDetail(data,isAdd,isView) {
			curentData = data;
			var mainform4room ;
			isView = isView;
			isAdd = isAdd;
				// 创建表单结构
				mainform4room = $("#mainform4meeting");
				mainform4room.ligerForm({
					inputWidth: 280,
		    		fields :   [
		    					{display : "用户名",name : "usercode",newline : true,labelWidth : 120,width : 220,space : 20,type : "text",validate : {required : true}	},
		    					{display : "密码",name : "password",newline : true,labelWidth : 120,width : 220,space : 20,type : "text",validate : {required : true}},
		    					{display : "姓名",name : "username",newline : true,labelWidth : 120,width : 220,space : 20,type : "text"},
		    					{display : "电话",name : "phone",newline : true,labelWidth : 120,width : 220,space : 20,type : "text"},
		    					{display : "年级",name : "dept",newline : true,labelWidth : 120,width : 220,space : 20,type : "select",comboboxName: "dept_",options: { valueFieldID: "dept", data: depts},validate : {required : true}},
		    					{display : "班级",name : "class_",newline : true,labelWidth : 120,width : 220,space : 20,type : "text"},
		    					isAdd?{display : "角色",name : "role",newline : true,labelWidth : 120,width : 220,space : 20,type : "select",comboboxName: "role_",options: { valueFieldID: "role", data: roles},validate : {required : true}}:{},
		    					{display : "性别",name : "sex",newline : true,labelWidth : 120,width : 220,space : 20,type : "select",comboboxName: "sex_",options: { valueFieldID: "sex", data: sexs},validate : {required : true}}
		    							
		    							],
		    		onAfterSetFields : function(){$(".l-group",mainform4room).width(1050);},
		    		readonly:isView,
					toJSON:JSON2.stringify
		    });
				
				detailWin = $.ligerDialog.open({
					target : $("#detailwindow4meeting"),
					width : 800,
					height : 520,
					title:'用户信息',
					top : 0,
					buttons : [ {
						text : '确定',
						onclick : function() {
							if(!isView){
								saveMeeting();
							}else{
								detailWin.hide();
							}
						}
					}, {
						text : '取消',
						onclick : function() {
							detailWin.hide();
						}
					} ]
				});
			if (curentData) {
				LG.loadDataForm(mainform4room,curentData);
			}
			function saveMeeting() {
				jQuery.metadata.setType("attr", "validate"); 
				 LG.validate(mainform4room, { debug: true });
				 if(mainform4room.valid()){
					 var savedata = {};
				    	savedata.usercode = $("#usercode").val();
				    	// 员工信息
				    	savedata.username = $("#username").val();
				    	savedata.password = $("#password").val();
				    	savedata.phone = $("#phone").val();
				    	savedata.sex = $("#sex").val();
				    	savedata.class_ = $("#class_").val();
				    	savedata.dept = $("#dept").val();
				    	savedata.role = $("#role").val();
						LG.ajax({
							loading : '正在保存数据中...',
							url: basepath+"user/"+(isAdd ? "insert.do"
									: "update.do"),
							data : savedata,
							success : function(data,Message) {
								LG.tip(Message);
									detailWin.hide();
									f_reload();
								
							},
							error : function(message) {
								LG.tip(message);
							}
						});
				 }
			}
		}
	  
	  
	// 加载toolbar
		var items = [];
		if(globalconfig.role!='4'&&globalconfig.role!='3'){
		items.push({ text: '新增',id:'add', click: toolbarBtnItemClick ,img: basepath + 'resource/icons/silkicons/add.png'});
		items.push({ line:true });
		items.push({ text: '删除',id:'delete',click: toolbarBtnItemClick, img: basepath+"resource/icons/miniicons/page_user_light.gif" });
		items.push({ line:true });
		}
		items.push({ text: '修改',id:'modify',click: toolbarBtnItemClick, img: basepath+"resource/icons/miniicons/page_edit.gif" });
		
	grid.toolbarManager.set('items', items);
	function f_reload() {
		grid.loadData();
	}
	 function f_delete(deldata) {
			LG.ajax({
				url:basepath+'user/delete.do',
				loading : '正在删除中...',
				data : {
					usercode : deldata
				},
				success : function(data,message) {
					LG.showSuccess(message);
					grid.loadData();
				},
				error : function(message) {
					LG.showError(message);
				}
			});
		}
	
});




 